﻿using MediatR;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.RabitsWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.RabitsWay
{
    public class DelAllRabitsCommandHandler : IRequestHandler<DelAllRabitsCommand, ApiResult<int>>
    {
        IBaseReponsitory<Rabits> Rabits;

        public DelAllRabitsCommandHandler(IBaseReponsitory<Rabits> Rabits)
        {
            this.Rabits = Rabits;
        }

        public async Task<ApiResult<int>> Handle(DelAllRabitsCommand request, CancellationToken cancellationToken)
        {
            //根据request.ids数组删除Rabits表中的数据
            var ids = request.ids.Split(',').Select(int.Parse).ToList();
            foreach (var id in ids)
            {
                var list = Rabits.GetQuery().FirstOrDefault(x => x.Rabit_Id == id);
                list.Rabit_IsDel = true;
                await Rabits.UpdateAsync(list);
            }
            return new ApiResult<int> 
            {
                Code = ApiEnum.Success,
                Message="删除成功了",
                Data = 1
            };
        }
    }
}
